package airthmetic.exercise.tree;

import java.util.ArrayList;
import java.util.List;

public class _145_二叉树的后序遍历 {

    public static List<Integer> postorderTraversal(TreeNode root) {
        if(root == null){
            return new ArrayList<Integer>();
        }
        List<Integer> list = new ArrayList();
        recur(root, list);

        return list;
    }

    public static void recur(TreeNode node, List<Integer> list){
        // terminal
        if(node==null){
            return;
        }
        // process current logic
        recur(node.left,list);
        recur(node.right,list);
        list.add(node.val);


    }

    public static void main(String[] args) {
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode = new TreeNode(1, treeNode2, treeNode3);
        System.out.println(treeNode);

        System.out.println(postorderTraversal(treeNode));
    }
}
